package com.wteam.controller.common;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wteam.framework.common.cache.Cache;
import com.wteam.framework.common.enums.ResultCode;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.framework.common.security.AuthUser;
import com.wteam.framework.common.security.context.UserContext;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.file.entity.dos.File;
import com.wteam.framework.modules.file.mapper.FileMapper;
import com.wteam.framework.modules.file.service.FileService;

import com.wteam.framework.modules.member.mapper.mapper.SdMemberMapper;
import com.wteam.framework.modules.member.service.SdMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 文件管理管理接口
 *
 * @author Chopper
 * @since 2020/11/26 15:41
 */
@RestController
@Tag(name = "文件管理接口")
@RequestMapping("/common/file")
public class FileController {

    @Autowired
    private SdMemberMapper sdMemberMapper;
    @Autowired
    private SdMemberService sdMemberService;
    @Autowired
    private FileService fileService;
    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private Cache cache;

    @Operation(summary = "获取自己的图片资源")
    @GetMapping("/getfile")
    @Parameter(name = "title", description = "名称模糊匹配")
    public File getFileList(@RequestHeader String uuid) {

        QueryWrapper<File> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", uuid);
//        LambdaQueryWrapper<SdMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SdMember::getPhoneNumber, mobilePhone);
        File file = fileMapper.selectOne(queryWrapper);
        if (file!=null){
            return ResultUtil.data(file).getResult();
        }else {
            throw new ServiceException(ResultCode.FILE_EXIST);
        }


        //只有买家才写入自己id



    }


    @Operation(summary = "文件重命名")
    @PostMapping(value = "/rename")
    public ResultMessage<File> upload(@RequestHeader String accessToken, String id, String newName) {

        AuthUser authUser = UserContext.getAuthUser(cache, accessToken);
        File file = fileService.getById(id);
        file.setName(newName);
        //操作图片属性判定
        switch (authUser.getRole()) {
            case MEMBER:
                if (file.getOwnerId().equals(authUser.getId()) && file.getUserEnums().equals(authUser.getRole().name())) {
                    break;
                }
                throw new ServiceException(ResultCode.USER_AUTHORITY_ERROR);
            case MANAGER:
                break;
            default:
                throw new ServiceException(ResultCode.USER_AUTHORITY_ERROR);
        }
        fileService.updateById(file);
        return ResultUtil.data(file);
    }

    @Operation(summary = "文件删除")
    @DeleteMapping(value = "/delete/{ids}")
    public ResultMessage delete(@RequestHeader String accessToken, @PathVariable List<String> ids) {

        AuthUser authUser = UserContext.getAuthUser(cache, accessToken);
        fileService.batchDelete(ids, authUser);
        return ResultUtil.success();
    }

}
